﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Management.Instrumentation;
using System.IO;

namespace KeyMaker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string keyinfo = GetCPUInfo() + GetHDDInfo() + GetMAC();
            string key = Convert.ToBase64String(Encoding.UTF8.GetBytes(keyinfo));

            textBox1.Text = keyinfo;
            textBox2.Text = key;

            //string decodedKey = Encoding.UTF8.GetString(Convert.FromBase64String(key));
            //label1.Text += decodedKey;
            //if (keyinfo == decodedKey)
            //    MessageBox.Show("ok");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"c:\windows\system32\msdate.dll";
            try
            {
                using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default))
                {
                    sw.Write(textBox2.Text);
                }
            }
            catch (Exception ex)
            {
                label1.Text = ex.ToString();
            }
            label1.Text = "Done!";
        }

        private string GetCPUInfo()
        {
            string cpuinfo = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuinfo += mo.Properties["ProcessorId"].Value.ToString();
                mo.Dispose();
            }
            moc.Dispose();
            mc.Dispose();
            return cpuinfo;
        }

        private string GetHDDInfo()
        {
            string HDid = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                HDid += mo.Properties["Model"].Value.ToString();
                mo.Dispose();
            }
            moc.Dispose();
            mc.Dispose();
            return HDid;
        }

        private string GetMAC()
        {
            string macInfo = string.Empty;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                    macInfo += mo["MacAddress"].ToString();
                mo.Dispose();
            }
            moc.Dispose();
            mc.Dispose();
            return macInfo;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;   //  获得用户选择的文件

                string srs = string.Empty;
                try
                {
                    using (StreamReader sr = new StreamReader(filePath, Encoding.Default))
                    {
                        srs = sr.ReadToEnd();
                    }
                    textBox3.Text = srs;
                    string decoded = Encoding.UTF8.GetString(Convert.FromBase64String(srs));
                    StringBuilder sb = new StringBuilder();
                    foreach (char c in decoded)
                    {
                        sb.Insert(0, c);
                    }
                    string oriInfo = sb.ToString();
                    string key = Convert.ToBase64String(Encoding.UTF8.GetBytes(oriInfo));
                    sb = new StringBuilder();
                    foreach (char c in key)
                    {
                        sb.Insert(0, c);
                    }
                    string keyReserved = sb.ToString();
                    textBox4.Text = keyReserved;
                }
                catch (Exception ex)
                {
                    label2.Text = ex.ToString();
                    return;
                }
            }
            else
            {
                label2.Text = "Key file not found!!";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string path = @"keyfile.dat";
            try
            {
                using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default))
                {
                    sw.Write(textBox4.Text);
                }
            }
            catch (Exception ex)
            {
                label2.Text = ex.ToString();
            }
            label2.Text = "Done!";
        }
    }
}
